$(document).ready(function(){
	$("#tabs").tabs();
	validateInput();
	$("#order_table").dataTable({
				        "bPaginate": false,
				        "bLengthChange": false,
				        "bFilter": false,
				        "bSort": false,
				        "bInfo": false,
				        "bAutoWidth": false
				    });
	$("#plan_setting").dataTable({
				        "bPaginate": false,
				        "bLengthChange": false,
				        "bFilter": false,
				        "bSort": false,
				        "bInfo": false,
				        "bAutoWidth": false
				    });
    $("#reservation_setting").dataTable({
        "bPaginate": false,
        "bLengthChange": false,
        "bFilter": false,
        "bSort": false,
        "bInfo": false,
        "bAutoWidth": false
     });
});



function addRowInOrder()
{
	$("#admin_orders").validate();
	if ($("#admin_orders").valid())
	{
	$('#order_table').dataTable().fnAddData( [
		$("#plan_number").val(),
		$("#table_number").val(),
		$("#seat_number").val(),
		"<input type=\"button\" onclick=\"removeRowInOrder($(this))\" value=\"-\" />" ] );
	}
}

function removeRowInOrder(row)
{
	var oTable = $('#order_table').dataTable();
	oTable.fnDeleteRow(oTable.fnGetPosition(row.closest('tr').get(0)))
}

function orderSeats()
{
	
	var oTable = $('#order_table').dataTable();
	var tableData = oTable.fnGetData();
	var nOfRows = tableData.length;
	
	if (nOfRows > 1)
	{
		tableData.shift();
		
		$.ajax({
				
			data: {
				action: "order",
				controller: "order_administration",
				data: tableData,
				orderForUser: $("#name_order").val()	
			},
			success: function(data){
				if (data.success == 0)
				{
					alert(data.message)
				}
				else
				{
					for( i=nOfRows; i >= 1; i--){
					  oTable.fnDeleteRow(i);
					}
					alert("Objednané");
					
			    }
				console.log(data.message);
			},
			error: function(data){
				console.log(data);
			}
		})
	}
	
}

function unreserveSeat()
{
	$.ajax({
				
			data: {
				action: "unreserveSeat",
				controller: "order_administration",
				planId: $("#plan_settings").val(),
				tableId: $("#table_id").val(),
				seatId: $("#seat_id").val()
				
			},
			success: function(data){
				alert(data.message);
				console.log(data.message);
			},
			error: function(data){
				alert(data.message);
				console.log(data);
			}
		})
}

function addNewRowInPlan()
{
	$.ajax({
				
			data: {
				action: "addRowToPlan",
				controller: "order_administration",
				planId: $("#plan_selection").val()
			},
			success: function(data){
				
				console.log(data.message);
			},
			error: function(data){
				console.log(data);
			}
		})
	
}

var linkToPlan;

function selectPlan(){
	var selectedPlan = $("#plan_selection").val();
	window.location = linkToPlan + "?plan_id=" + selectedPlan;
}

function changeStatusToPaid(orderId)
{
	
	$.ajax({
			
			data: {
				action: "changeToPaid",
				controller: "order_administration",
				order: orderId	
			},
			success: function(data){
				if (data.success == 0)
				{
					alert(data.message)
				}
				else
				{
					var orderRow = $("#" + orderId);
					var orderSpan = orderRow.find("span");
					$(orderSpan).show();
					var orderButton = orderRow.find("input").remove();
			    }
				console.log(data.message);
			},
			error: function(data){
				console.log(data);
			}
		})
}
function validateInput()
{
	$('#admin_orders').validate({
			rules: {
				table_number : {
					required: true,
					number: true
				},
				seat_number : {
					required : true,
				    number: true
				},
				name_order : {
					required : true,
					
				},
				
			},
			messages : {
				table_number : {
					required: "Required",
					number: "Zadajte číslo"
				},
				seat_number : {
					required : "Required",
				    number: "Zadajte číslo"
				},
				name_order : {
					required :"Required",
					
				}, 
			},
			
		});
}

